package isgesis;

import java.sql.*;

/**
 *
 * @author Agustin
 */
public class Conexion {

    private static Conexion instance;
    private Connection conexion;
    private String url = "jdbc:mysql://localhost:3306/gesis";
    private String user = "root";
    private String pass = "";

    private Conexion() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public static Conexion getInstance() {
        if (instance == null) {
            instance = new Conexion();
        }
        return instance;
    }

    public void conectar() throws SQLException {
        try {
            conexion = DriverManager.getConnection(url, user, pass);

        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public void desconectar() throws SQLException {
        try {
            conexion.close();
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public ResultSet select(String query) throws SQLException {
        try {
            Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery(query);
            return rs;
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }

    public void insert(String query) throws SQLException {
        try {
            Statement st = conexion.createStatement();
            st.execute(query);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e.toString());
        }
    }
}
